<?php
//пароль
if (!defined('INWEB')) header("Location: ../index.php");
$a=getVar('a');
$id=getVar('id','int',true);
includeLang('menu');
switch($a)
{
    case 'add':
        if($_POST)
        {
            $l=getVar('lang');
            $link=getVar('link');
            $access=getVar('access');
            $parent=getVar('parent');
            $parent=SQLNull($parent);
            $sql->query("INSERT INTO `menu` (`lang`,`link`,`access`,`parent_id`) VALUES ('$l', '$link','$access',$parent);");
            if($sql->num_rows())
            {
                msg($Lang['success'],$Lang['menuitem_added']);
            }
            else
            {
                msg($Lang['error'], $Lang['failed_add_menuitem'],'error');
            }
            echo '<br /><a href="?p='.$page.'">'.$Lang['back'].'</a>';
        }
        else
        {
            echo '<form action="?p='.$page.'&a=add" method="post"><table border="1">';
            echo '<tr><th>'.$Lang['lang'].'</th><td><input type="text" name="lang" /></td></tr>';
            echo '<tr><th>'.$Lang['link'].'</th><td><input type="text" name="link" /></td></tr>';
            ?>
            <tr><th><?php echo $Lang['access'];?></th><td><select name="access">
            <?php
            $aqry=$sql->query("DESCRIBE `groups`;");
            while($access=$sql->fetch_array($aqry))
            {
                echo '<option value="'.$access['Field'].'">'.$access['Field'].'</option>';
            }
            ?>
            </select></td></tr>
            <tr><th><?php echo $Lang['parent'];?></th><td><select name="parent">
            <option value=""><?php echo $Lang['main_item'];?></option>
            
            <?php
            $pqry=$sql->query("SELECT `id`, `lang` FROM `menu` WHERE `parent_id` IS NULL");
            while($parent=$sql->fetch_array($pqry))
            {
                echo '<option value="'.$parent['id'].'">'.$parent['lang'].'</option>';
            }
            echo '<tr><th></th><td><input type="submit" value="'.$Lang['add'].'" /></td></tr>';
            echo '</table></form>';
        }
    break;
    case 'edit':
        if($_POST)
        {
            $l=getVar('lang');
            $link=getVar('link');
            $access=getVar('access');
            $parent=getVar('parent');
            $parent=SQLNull($parent);
            $sql->query("UPDATE `menu` SET `lang`='$l', `link`='$link',`access`='$access',`parent_id`=$parent WHERE `id`='$id';");
            if($sql->num_rows())
            {
                msg($Lang['success'],sprintf($Lang['menuitem_updated'],$id));
            }
            else
            {
                msg($Lang['error'], $Lang['failed_update_menuitem'].$id.'.','error');
            }
            echo '<br /><a href="?p='.$page.'">'.$Lang['back'].'</a>';
        }
        else
        {
            $sql->query("SELECT * FROM `menu` WHERE `id`='$id';");
            if(!$sql->num_rows()) break;
            $mi=$sql->fetch_array();
            echo '<form action="?p='.$page.'&a=edit&id='.$id.'" method="post"><table border="1">';
            echo '<tr><th>'.$Lang['lang'].'</th><td><input type="text" name="lang" value="'.$mi['lang'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['link'].'</th><td><input type="text" name="link" value="'.$mi['link'].'" /></td></tr>';
            ?>
            <tr><th><?php echo $Lang['access'];?></th><td><select name="access">
            <?php
            $aqry=$sql->query("DESCRIBE `groups`;");
            while($access=$sql->fetch_array($aqry))
            {
                echo '<option value="'.$access['Field'].'" '.(($access['Field']==$mi['access'])?'selected="selected"':'').'>'.$access['Field'].'</option>';
            }
            ?>
            </select></td></tr>
            <tr><th><?php echo $Lang['parent'];?></th><td><select name="parent">
            <option value="" <?php echo ($mi['parent_id']=='')?'selected="selected"':'';?>><?php echo $Lang['main_item'];?></option>
            
            <?php
            $pqry=$sql->query("SELECT `id`, `lang` FROM `menu` WHERE `parent_id` IS NULL AND `id`!='{$mi['id']}'");
            while($parent=$sql->fetch_array($pqry))
            {
                echo '<option value="'.$parent['id'].'" '.(($parent['id']==$mi['parent_id'])?'selected="selected"':'').'>'.$parent['lang'].'</option>';
            }
            echo '<tr><th></th><td><input type="submit" value="'.$Lang['update'].'" /></td></tr>';
            echo '</table></form>';
        }
    break;
    case 'del':
        if(isset($_GET['confirm']))
        {
            $sql->query("DELETE FROM `menu` WHERE `id`='$id'");
            if($sql->num_rows())
            {
                msg($Lang['success'], sprintf($Lang['menuitem_deleted'],$id));
            }
            else
            {
                msg($Lang['error'], $Lang['failed_delete_menuitem'].$id.'.','error');
            }
            
        }
        else
        {
            msg($Lang['warning'],$Lang['are_u_sure_delete_menuitem'].$id.'?<br /> <a href="?p='.$page.'&a=del&id='.$id.'&confirm">'.$Lang['yes'].'</a>/<a href="?p='.$page.'">'.$Lang['no'].'</a>','warning');
        }
        echo '<br /><a href="?p='.$page.'">'.$Lang['back'].'</a>';
    break;
    default:
        echo '<table border="1"><tr><th>'.$Lang['id'].'</th><th>'.$Lang['lang'].'</th><th>'.$Lang['link'].'</th><th>'.$Lang['access'].'</th><th>'.$Lang['actions'].'<a href="?p='.$page.'&a=add"><img src="img/add.png" title="'.$Lang['add'].'" alt="'.$Lang['add'].'" /></a></th></tr>';
        $qry=$sql->query("SELECT * FROM `menu` WHERE `parent_id` IS NULL;");
        while($main=$sql->fetch_array($qry))
        {
            echo '<tr><td>'.$main['id'].'</td><td>'.$main['lang'].'</td><td>'.$main['link'].'</td><td>'.$main['access'].'</td><td><a href="?p='.$page.'&a=edit&id='.$main['id'].'"><img src="img/edit.png" title="'.$Lang['edit'].'" alt="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=del&id='.$main['id'].'"><img src="img/delete.png" title="'.$Lang['delete'].'" alt="'.$Lang['delete'].'" /></a></td></tr>';
            $sqry=$sql->query("SELECT * FROM `menu` WHERE `parent_id`='{$main['id']}'");
            if($sql->num_rows($sqry))
            {
                echo '<tr><td>'.$Lang['subpages'].'</td><td colspan="4"><table border="1"><tr><th>'.$Lang['id'].'</th><th>'.$Lang['lang'].'</th><th>'.$Lang['link'].'</th><th>'.$Lang['access'].'</th><th>'.$Lang['parent_id'].'</th><th>'.$Lang['actions'].'</th></tr>';
                while($smain=$sql->fetch_array($sqry))
                {
                    echo '<tr><td>'.$smain['id'].'</td><td>'.$smain['lang'].'</td><td>'.$smain['link'].'</td><td>'.$smain['access'].'</td><td>'.$smain['parent_id'].'</td><td><a href="?p='.$page.'&a=edit&id='.$smain['id'].'"><img src="img/edit.png" title="'.$Lang['edit'].'" alt="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=del&id='.$smain['id'].'"><img src="img/delete.png" title="'.$Lang['delete'].'" alt="'.$Lang['delete'].'" /></a></td></tr>';
                }
                echo '</table></td></tr>';
            }
        }
        echo '</table>';
    break;
}
?>